﻿using System;
using System.Collections.Generic;
using DomainModel;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace DataAccessLayer.AuctioneerRepository
{
    public class AuctioneerRepository : IAuctioneerRepository
    {
        public AuctioneerRepository()
        {
            DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
        }

        public void Add(Auctioneer auctioneer)
        {
            ValidationResults validationResults = Validation.Validate(auctioneer);
            if (!validationResults.IsValid)
            {
                string errorMessage = string.Empty;
                foreach (var current in validationResults)
                {
                    errorMessage += current.Message + "\n";
                }

                throw new ArgumentException(errorMessage);
            }

            Database database = DatabaseFactory.CreateDatabase("MyConStr");
            using (DbCommand command = database.GetStoredProcCommand("AddAuctioneer"))
            {
                database.AddInParameter(command, "@username", DbType.String, auctioneer.Username);
                database.AddInParameter(command, "@firstname", DbType.String, auctioneer.FirstName);
                database.AddInParameter(command, "@lastname", DbType.String, auctioneer.LastName);
                database.AddInParameter(command, "@password", DbType.String, auctioneer.Password);
                database.AddInParameter(command, "@rating", DbType.Int32, auctioneer.Rating);
                database.AddOutParameter(command, "@id", DbType.Int32, 255);

                try
                {
                    database.ExecuteNonQuery(command);
                    auctioneer.Id = (int)database.GetParameterValue(command, "@id");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace);
                }
            }
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public void Edit(Auctioneer auctioneer)
        {
            throw new NotImplementedException();
        }

        public List<Auctioneer> GetAll()
        {
            List<Auctioneer> auctioneerList = new List<Auctioneer>();
            Database database = DatabaseFactory.CreateDatabase("MyConStr");
            using (IDataReader reader = database.ExecuteReader(CommandType.StoredProcedure, "GetAllAuctioneers"))
            {
                while (reader.Read())
                {
                    Auctioneer auctioneer = new Auctioneer();
                    auctioneer.FirstName = reader["FirstName"]?.ToString();
                    auctioneer.Id = (int)reader["Id"];
                    auctioneer.LastName = reader["LastName"]?.ToString();
                    auctioneer.Password = reader["Password"]?.ToString();
                    auctioneer.Rating = (decimal)reader["Rating"];
                    auctioneer.Username = reader["Username"]?.ToString();
                    auctioneerList.Add(auctioneer);
                }
            }

            return auctioneerList;
        }
    }
}
